package com.ning.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ning.reggie.common.R;
import com.ning.reggie.dto.OrdersDto;
import com.ning.reggie.entity.Orders;
import com.ning.reggie.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
@Api(tags="订单相关接口")
public class OrdersController{

    @Autowired
    private OrdersService ordersService;

    /**
     * 分页管理查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    @Cacheable(value="orderCache")
    @ApiOperation(value = "分页查询")
    public R<Page> pageR(int page, int pageSize, Long userId){
        log.info("page={},pageSize={},name={}",page,pageSize,userId);
        //构造分页构造器
        Page<Orders> pageInfo = new Page(page,pageSize);

        Page<OrdersDto> pagedto = new Page();
        //构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(userId != null,Orders::getUserId,userId);
        //添加排序条件
        queryWrapper.orderByDesc(Orders::getOrderTime);

        //执行查询
        ordersService.page(pageInfo,queryWrapper);

        BeanUtils.copyProperties(pageInfo,pagedto,"records");
        List<Orders> records = pageInfo.getRecords();
//        将订单列表转为ordersDto
        List<OrdersDto> dtos = records.stream().map((item->{
            OrdersDto dto = new OrdersDto();
            BeanUtils.copyProperties(item,dto);
            Long orderId = item.getId();
            Orders order = ordersService.getById(orderId);
            dto.setUserName(order.getConsignee());
            return dto;
        })).collect(Collectors.toList());

        pagedto.setRecords(dtos);

        return R.success(pagedto);

    }


    @PutMapping
    @CacheEvict(value="orderCache",allEntries=true)
    @ApiOperation(value = "订单状态")
    public R<String> paiSong(@RequestBody Orders order){
        int status = order.getStatus();
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getId,order.getId());
        order = ordersService.getOne(queryWrapper);
        order.setStatus(status);
        ordersService.updateById(order);
        return R.success("ok");
    }

    /**
     * 用户下单。
     *
     * @param order 包含订单信息的 Orders 对象，从请求体中获取。
     * @return 返回包含下单结果的通用响应对象 R，泛型参数为 String。
     */
    @PostMapping("/submit")
    @CacheEvict(value="orderCache",allEntries=true)
    @ApiOperation(value = "用户下单")
    public R<String> submit(@RequestBody Orders order) {
        log.info("订单数据：{}", order);
        // 调用订单服务类的 submit 方法处理订单提交逻辑。
        ordersService.submit(order);
        return R.success("下单成功");
    }

}
